' --- Copyright (c) notice DnnConsulting ---
'  Copyright (c) 2013 Dnn Consulting.  www.DnnConsulting.nl. BSD License.
' Author: D.C.Lee
' ------------------------------------------------------------------------
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' ------------------------------------------------------------------------
' This copyright notice may NOT be removed, obscured or modified without written consent from the author.
' --- End copyright notice --- 

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports System.IO
Imports DotNetNuke.Entities.Modules

Partial Class Settings
    Inherits Entities.Modules.ModuleSettingsBase

#Region "Base Method Implementations"

    Public Overrides Sub LoadSettings()
        Try
            If (Page.IsPostBack = False) Then

                Dim settings As Hashtable = Portals.PortalSettings.GetModuleSettings(ModuleId)

                If Not CType(settings("DNNC_PGALLERY_USERNAME"), String) Is Nothing Then txtPicasaUserName.Text = CType(settings("DNNC_PGALLERY_USERNAME"), String)
                If Not CType(settings("DNNC_PGALLERY_PASSWORD"), String) Is Nothing Then txtPicasaPassWord.Text = CType(settings("DNNC_PGALLERY_PASSWORD"), String)

                If Not CType(settings("DNNC_PGALLERY_GALSCRIPT"), String) Is Nothing Then
                    txtGallerifficScript.Text = CType(settings("DNNC_PGALLERY_GALSCRIPT"), String)
                Else
                    txtGallerifficScript.Text = LoadFile("galscript")
                End If

                If Not CType(settings("DNNC_PGALLERY_GALCSS"), String) Is Nothing Then
                    txtGallerifficCss.Text = CType(settings("DNNC_PGALLERY_GALCSS"), String)
                Else
                    txtGallerifficCss.Text = LoadFile("galcss")
                End If

                If Not CType(settings("DNNC_PGALLERY_CATSTYLE"), String) Is Nothing Then
                    txtCatStyle.Text = CType(settings("DNNC_PGALLERY_CATSTYLE"), String)
                Else
                    txtCatStyle.Text = LoadFile("galcatstyle")
                End If


                If Not CType(settings("DNNC_PGALLERY_CATSTYLE"), String) Is Nothing Then txtCatStyle.Text = CType(settings("DNNC_PGALLERY_CATSTYLE"), String)
                If Not CType(settings("DNNC_PGALLERY_BACKTOALBUMS"), String) Is Nothing Then txtCatBackToAlbums.Text = CType(settings("DNNC_PGALLERY_BACKTOALBUMS"), String)

            End If
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    Public Overrides Sub UpdateSettings()
        Try
            Dim objModules As New Entities.Modules.ModuleController

            objModules.UpdateModuleSetting(ModuleId, "DNNC_PGALLERY_USERNAME", txtPicasaUserName.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNC_PGALLERY_PASSWORD", txtPicasaPassWord.Text)

            objModules.UpdateModuleSetting(ModuleId, "DNNC_PGALLERY_GALSCRIPT", txtGallerifficScript.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNC_PGALLERY_GALCSS", txtGallerifficCss.Text)

            objModules.UpdateModuleSetting(ModuleId, "DNNC_PGALLERY_CATSTYLE", txtCatStyle.Text)
            objModules.UpdateModuleSetting(ModuleId, "DNNC_PGALLERY_BACKTOALBUMS", txtCatBackToAlbums.Text)

        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

#End Region

#Region " Event methods"

    Private Sub btnGalScriptDefault_Click(sender As Object, e As System.EventArgs) Handles btnGalScriptDefault.Click
        Dim scr As String = LoadFile("galscript")
        txtGallerifficScript.Text = scr
    End Sub

    Private Sub btnGalCssDefault_Click(sender As Object, e As System.EventArgs) Handles btnGalCssDefault.Click
        Dim scr As String = LoadFile("galcatstyle")
        txtGallerifficCss.Text = scr
    End Sub

    Private Sub btnCatStyleDefault_Click(sender As Object, e As System.EventArgs) Handles btnCatStyleDefault.Click
        Dim scr As String = LoadFile("galcss")
        txtCatStyle.Text = scr
    End Sub

#End Region

#Region " Private methods "

    Private Function LoadFile(fileToLoad As String) As String
        Dim fStr As StringBuilder = New StringBuilder
        Dim fn As String = String.Empty

        Select Case fileToLoad
            Case "galcss" : fn = Server.MapPath(ControlPath & "files/Galleriffic_style.txt")
            Case "galscript" : fn = Server.MapPath(ControlPath & "files/Galleriffic_script.txt")
            Case "galcatstyle" : fn = Server.MapPath(ControlPath & "files/Category_style.txt")
        End Select

        Dim sr As StreamReader
        sr = File.OpenText(fn)
        While sr.Peek() <> -1
            fStr.AppendLine(sr.ReadLine())
        End While
        sr.Close()

        Return fStr.ToString
    End Function

#End Region




    
End Class

